import redis
# import aioredis
import json
import settings


class RedisMemory:
    def __init__(self, host=settings.redis_host, port=settings.redis_port, db=settings.redis_db,
                 password=settings.redis_pwd):
        self.redis_client = redis.Redis(
            host=host,
            port=port,
            db=db,
            password=password,
            decode_responses=True
        )

    def set_user_info_memory(self, session_id, value):
        value_json = json.dumps(value, ensure_ascii=False, default=list, )
        if not self.exist_key(session_id):
            self.redis_client.hset(session_id, 'user_info', '{}')
        self.redis_client.hset(session_id, 'user_info', value_json)

    def get_user_info_memory(self, session_id):
        value_json = self.redis_client.hget(session_id, 'user_info')
        if value_json:
            return json.loads(value_json)
        return None

    def set_solution_memory(self, session_id, value):
        value_json = json.dumps(value, default=list, ensure_ascii=False)
        if not self.exist_key("planner:solution:" + session_id):
            self.redis_client.hset("planner:solution:" + session_id, 'solution', '{}')
        self.redis_client.hset("planner:solution:" + session_id, 'solution', value_json)

    def get_solution_memory(self, session_id):
        value_json = self.redis_client.hget("planner:solution:" + session_id, 'solution')
        if value_json:
            return json.loads(value_json)
        return None

    def get_memory(self, hkey, key):
        value_json = self.redis_client.hget(hkey, key)
        if value_json:
            return json.loads(value_json)
        return None

    def set_memory(self, hkey, key, value):
        value_json = json.dumps(value, default=list, ensure_ascii=False)
        if not self.exist_key(hkey):
            self.redis_client.hset(hkey, key, '{}')
        self.redis_client.hset(hkey, key, value_json)

    def exist_key(self, session_id):
        return self.redis_client.exists(session_id)

    def hexist_key(self, hkey, key):
        return self.redis_client.hexists(hkey, key)

    def hdel(self, hkey, key):
        return self.redis_client.hdel(hkey, key)

    def get(self, key):
        return self.redis_client.get(key)

    def keys(self, pattern):
        return self.redis_client.keys(pattern)

    def sadd(self, key, value):
        return self.redis_client.sadd(key, value)

    def smembers(self, key):
        return self.redis_client.smembers(key)

    def srem(self, key, value):
        return self.redis_client.srem(key, value)

    def delete(self, key):
        return self.redis_client.delete(key)

    def hgetall(self, hkey):
        return self.redis_client.hgetall(hkey)

    def lpushall(self, key, values):
        # 使用循环调用 LPUSH 将多个值一次性推入列表的左侧
        for value in values:
            self.redis_client.lpush(key, value)

    def __getattr__(self, name):
        # 当调用不存在的方法时，将其委托给redis_client
        return getattr(self.redis_client, name)

    def set_keys_in_redis(self, insurance_name, key_mapping):
        # key_mapping是一个字典，包含chinese_key和english_key的映射关系
        for chinese_key, english_key in key_mapping.items():
            self.set_memory("product_dict:" + insurance_name, chinese_key, english_key)

    def find_and_remove_value(self, key_prefix, value_to_remove):
        cursor = '0'
        while cursor != 0:
            cursor, keys = self.redis_client.scan(cursor=cursor)
            for key in keys:
                if key.startswith(key_prefix) and self.redis_client.sismember(key, value_to_remove):
                    self.redis_client.srem(key, value_to_remove)

    def set_sop_send_msg_list(self, msg_data):
        self.redis_client.lpush('SET_SOP_SEND_MSG_LIST', msg_data)

    def get_sop_send_msg_list(self):
        return self.redis_client.rpop('SET_SOP_SEND_MSG_LIST')

    def get_sop_send_msg_status(self):
        return self.redis_client.get('SOP_SEND_MSG_STATUS')

    def set_sop_send_msg_status_stop(self):
        return self.redis_client.set('SOP_SEND_MSG_STATUS', 0)

    def remove_ql_wechat_msg(self):
        self.redis_client.delete('SET_SOP_SEND_MSG_LIST')


if __name__ == '__main__':
    redis_memory = RedisMemory()
    val=need_human_customer_service = redis_memory.get("qw_planner:"+ "need_human_customer_service:" + "2243")
    print(val=='0')
